﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using WebMovil.Base;

namespace WebMovil
{
    /// <summary>
    /// Summary description for Handler1
    /// </summary>
    public class Handler1 : IHttpHandler
    {
        public bool IsReusable
            {
                get
                {
                    return false;
                }
            }


        public void ProcessRequest(HttpContext context)
        {
            var db = new DataClasses1DataContext();
            
            string id = context.Request.QueryString["Id"];
            var r = (from a in db.MaestroArticulos where a.IdMaestroArticulo.ToString() == id select a).First();
            if (id != null && r.Foto != null)
            {
                context.Response.ContentType = "image/jpeg";
                Stream strm = ShowEmpImage(id);
                byte[] buffer = new byte[4096];
                int byteSeq = strm.Read(buffer, 0, 4096);
                while (byteSeq > 0)
                {
                    context.Response.OutputStream.Write(buffer, 0, byteSeq);
                    byteSeq = strm.Read(buffer, 0, 4096);
                }
            }
        }
        private Stream ShowEmpImage(string id)
        {

            var db = new DataClasses1DataContext();
            var r = (from a in db.MaestroArticulos where a.IdMaestroArticulo.ToString() == id select a).First();
            return new MemoryStream(r.Foto.ToArray());
        }
    }
}